package com.jfdesgagne.traklib.ui.components {
	import fl.controls.CheckBox;
	import fl.controls.NumericStepper;

	import com.jfdesgagne.traklib.collections.TrakConfiguration;
	import com.jfdesgagne.traklib.ui.events.TrakUIEvent;

	import flash.display.MovieClip;
	import flash.events.Event;

	/**
	 * @author jdesgagne
	 */
	public class TrakUICommonOptions extends MovieClip {
		public var blur:NumericStepper;
		public var contrast:NumericStepper;
		public var minBlob:NumericStepper;
		public var maxBlob:NumericStepper;
		public var brightness:NumericStepper;
		public var saturation:NumericStepper;		public var multiTracking:CheckBox;

		public function TrakUICommonOptions() {
		}

		public function addEvents():void {
			this.blur.addEventListener(Event.CHANGE, this.setValues);
			this.contrast.addEventListener(Event.CHANGE, this.setValues);
			this.minBlob.addEventListener(Event.CHANGE, this.setValues);			this.maxBlob.addEventListener(Event.CHANGE, this.setValues);
			this.brightness.addEventListener(Event.CHANGE, this.setValues);
			this.saturation.addEventListener(Event.CHANGE, this.setValues);			this.multiTracking.addEventListener(Event.CHANGE, this.setValues);
		}

		public function removeEvents():void {
			this.blur.removeEventListener(Event.CHANGE, this.setValues);
			this.contrast.removeEventListener(Event.CHANGE, this.setValues);
			this.minBlob.removeEventListener(Event.CHANGE, this.setValues);			this.maxBlob.removeEventListener(Event.CHANGE, this.setValues);
			this.brightness.removeEventListener(Event.CHANGE, this.setValues);
			this.saturation.removeEventListener(Event.CHANGE, this.setValues);			this.multiTracking.removeEventListener(Event.CHANGE, this.setValues);
		}

		public function setValues(e:Event):void {
			var valueType:String, value:int;
			switch( e.currentTarget ) {
				case this.blur :
					valueType = TrakConfiguration.BLUR;
					value = this.blur.value;
					break;
				
				case this.brightness :
					valueType = TrakConfiguration.BRIGHTNESS;
					value = this.brightness.value;
					break;
				
				case this.contrast :
					valueType = TrakConfiguration.CONTRAST;
					value = this.contrast.value;
					break;
				
				case this.minBlob :
					valueType = TrakConfiguration.MIN_BLOB;
					value = this.minBlob.value;
					break;
				
				case this.maxBlob :
					valueType = TrakConfiguration.MAX_BLOB;
					value = this.maxBlob.value;
					break;
				
				case this.saturation :
					valueType = TrakConfiguration.SATURATION;
					value = this.saturation.value;
					break;
				
				case this.multiTracking :
					valueType = TrakConfiguration.MULTI_TRACKING;
					value = (this.multiTracking.selected == true) ? 1:0;
					break;
			}
			this.stage.focus = this.stage;
			this.dispatchEvent(new TrakUIEvent(TrakUIEvent.VALUE_CHANGED, valueType, value, true, true));
		}

		public function setCalibration():void {
			this.blur.value = TrakConfiguration.blur;
			this.contrast.value = TrakConfiguration.contrast;
			this.minBlob.value = TrakConfiguration.minBlob;			this.maxBlob.value = TrakConfiguration.maxBlob;
			this.brightness.value = TrakConfiguration.brightness;			this.saturation.value = TrakConfiguration.saturation;			this.multiTracking.selected = TrakConfiguration.multiTracking;
			
			this.dispatchEvent(new TrakUIEvent(TrakUIEvent.VALUE_CHANGED, TrakConfiguration.BLUR, this.blur.value, true, true));
			this.dispatchEvent(new TrakUIEvent(TrakUIEvent.VALUE_CHANGED, TrakConfiguration.BRIGHTNESS, this.brightness.value, true, true));
			this.dispatchEvent(new TrakUIEvent(TrakUIEvent.VALUE_CHANGED, TrakConfiguration.CONTRAST, this.contrast.value, true, true));			this.dispatchEvent(new TrakUIEvent(TrakUIEvent.VALUE_CHANGED, TrakConfiguration.SATURATION, this.saturation.value, true, true));
			this.dispatchEvent(new TrakUIEvent(TrakUIEvent.VALUE_CHANGED, TrakConfiguration.MIN_BLOB, this.minBlob.value, true, true));
			this.dispatchEvent(new TrakUIEvent(TrakUIEvent.VALUE_CHANGED, TrakConfiguration.MAX_BLOB, this.maxBlob.value, true, true));			this.dispatchEvent(new TrakUIEvent(TrakUIEvent.VALUE_CHANGED, TrakConfiguration.MULTI_TRACKING, (this.multiTracking.selected == true) ? 1:0, true, true));
		}
	}
}
